
package com.benimetum.managers;

import com.benimetum.database.Sparql;
import com.benimetum.exceptions.DatabaseConnectException;
import com.benimetum.exceptions.DatabaseWriteOrDeleteException;
import com.benimetum.items.Course;
import com.benimetum.items.Student;
import com.benimetum.items.Teacher;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Ethem Cem Ozkan
 * @email ethemcem.ozkan@gmail.com
 * No 07110604
 */
public class CourseManager
{
    private final static String RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    private final static String ETU="http://www.benimetum.com/benimetum.owl#";
    
    /**
     * Creates course
     * @param id
     * @param name
     * @param teacherId
     * @param studentId
     * @return 
     */
    public static boolean createCourse(String id, String name, String teacherId, String[] studentId){
	  try {
		Sparql.insertProperty(ETU+"c"+id, RDF+"type", ETU+"Course");
		Sparql.insertLiteral(ETU+"c"+id, ETU+"id", "c"+id);
		Sparql.insertLiteral(ETU+"c"+id, ETU+"name", name);
		Sparql.insertProperty(ETU+"c"+id, ETU+"hasTeacher", ETU+teacherId);
		Sparql.insertProperty(ETU+teacherId, ETU+"isTeacherOf", ETU+"c"+id);
		for (int i = 0; i < studentId.length; i++) {
		    StudentManager.enrollStudent("c"+id,studentId[i]);
		}
		ForumManager.createForum("c"+id);
	  } catch (DatabaseConnectException ex) {
		return false;
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  }
	  return true;
    }
    
    /**
     * Derse kayıtlı olan öğrencileri array olarak döndürür
     * @param courseId
     * @return 
     */
    public static Student[] getCoursesStudents(String courseId){
	  String query="select ?email ?id ?firstName ?lastName ?status ?picture ?mentor where {?course rdf:type etu:Course. ?course etu:id \""+courseId+"\". ?course etu:hasStudents ?student. "
		    + "?student etu:email ?email. ?student etu:id ?id. ?student foaf:firstName ?firstName. ?student etu:status ?status. ?student foaf:surname ?lastName. "
		    + "?student etu:profilePicture ?picture. ?student etu:hasMentor ?teacher. ?teacher etu:id ?mentor.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  ArrayList<Student> students=new ArrayList<Student>();
	  while(iterator.hasNext()){
		QuerySolution next = iterator.next();
		Teacher teacher = TeacherManager.getTeacherById(next.get("mentor").toString());
		students.add(new Student(next.get("email").toString(), next.get("id").toString(), next.get("firstName").toString(), next.get("lastName").toString(), toList.get(0).get("status").toString(), next.get("picture").toString(), teacher));
	  }
	  Student[] array=new Student[students.size()];
	  return students.toArray(array);
    }
    
    public static Course getCourse(String courseId){
	  String query="select ?name ?teacherId where{?course rdf:type etu:Course. ?course etu:id \""+courseId+"\"."
		    + " ?course etu:name ?name. ?course etu:hasTeacher ?teacher. ?teacher etu:id ?teacherId.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  if(!toList.isEmpty())
		return new Course(courseId, toList.get(0).get("name").toString(), TeacherManager.getTeacherById(toList.get(0).get("teacherId").toString()), getCoursesStudents(courseId));
	  return null;
    }
    
    public static Course[] getCourses(){
	  String query="select ?id ?name ?teacherId where{?course rdf:type etu:Course. ?course etu:id ?id"
		    + " ?course etu:name ?name. ?course etu:hasTeacher ?teacher. ?teacher etu:id ?teacherId.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  ArrayList<Course> courses=new ArrayList<Course>();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  while (iterator.hasNext()) {
		QuerySolution querySolution = iterator.next();
		courses.add(new Course(querySolution.get("id").toString(), querySolution.get("name").toString(), TeacherManager.getTeacherById(querySolution.get("teacherId").toString()), getCoursesStudents(querySolution.get("id").toString())));
	  }
	  Course[] array=new Course[courses.size()];
	  return courses.toArray(array);
    }
    
    public static Course[] getStudentsCourse(String studentId){
	  String query="select ?id ?name ?teacherId where{?student rdf:type etu:Student. ?student etu:id \""+studentId+"\".?student etu:hasCourse ?course. ?course etu:id ?id."
		    + "?course etu:name ?name. ?course etu:hasTeacher ?teacher. ?teacher etu:id ?teacherId.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  ArrayList<Course> courses=new ArrayList<Course>();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  while (iterator.hasNext()) {
		QuerySolution querySolution = iterator.next();
		courses.add(new Course(querySolution.get("id").toString(), querySolution.get("name").toString(), TeacherManager.getTeacherById(querySolution.get("teacherId").toString()), getCoursesStudents(querySolution.get("id").toString())));
	  }
	  Course[] array=new Course[courses.size()];
	  return courses.toArray(array);
    }
    
    public static Teacher getCoursesTeacher(String courseId){
	  String query="select ?id ?email ?firstName ?surname ?status ?picture "
		    + "where {?course rdf:type etu:Course. ?course etu:id \""+courseId+"\". ?course etu:hasTeacher ?teacher. ?teacher etu:id ?id. ?teacher etu:email ?email."
		    + "?teacher foaf:firstName ?firstName. ?teacher foaf:surname ?surname. ?teacher etu:status ?status."
		    + "?teacher etu:profilePicture ?picture.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Teacher teacher=new Teacher(toList.get(0).get("email").toString(), toList.get(0).get("id").toString(), toList.get(0).get("firstName").toString(), toList.get(0).get("surname").toString(), toList.get(0).get("status").toString(), toList.get(0).get("picture").toString());
	  return teacher;
    }
    
    public static boolean addCourseFile(File file, String fileInfo,String courseId){
	  return FileManager.addFile(file, fileInfo, courseId);
    }
    
    public static boolean removeCourseFile(String fileId){
	  return FileManager.removeFile(fileId);
    }

}
